"""
@Description :   DINO-XSEEK 模型 API
@Author      :   tqychy 
@Time        :   2025/08/24 16:09:48
"""
from dino_x import DinoX_API
from dds_cloudapi_sdk.tasks.v2_task import \
    create_task_with_local_image_auto_resize
import sys

sys.path.append("./nets")


class DinoXSEEK_API(DinoX_API):
    def __call__(self, inputs):
        task = create_task_with_local_image_auto_resize(
            api_path=self.model_path,
            api_body_without_image=inputs[0],
            image_path=inputs[1],
        )
        self.model.run_task(task)
        response = task.result
        response = response["objects"]
        if len(response) == 0:
            raise ValueError("DINO-XSEEK 模型未检测到目标")
        return str(response[0]["bbox"]) if self.cfg.EVAL.TYPE == "rec" else response

    @staticmethod
    def make_prompt(image_path, sentence, category, mode):
        if mode == "rec":
            prompt = f"""
            <image>
            Here is a description of the objects in the figure. 
            Please enclose the corresponding positions using coordinate boxes. 
            Examples of coordinate value formats: [x1, y1, x2, y2]
            Please only output boxes in a fixed format like:
            ```json\n[\n\t{{"bbox_2d": [x1, y1, x2, y2], "label": "..."}}\n]\n```
            and do not output any unnecessary content.

            Description:\n{sentence}
            """
        elif mode == "detect":
            raise ValueError(f"DINO-XSEEK 不支持 object detect 任务。")
        else:
            raise ValueError(f"Unknown task type! {mode}")

        api_body = {
            "model": "DINO-XSeek-1.0",
            "prompt": {
                "type": "text",
                "text": prompt
            },
            "targets": ["bbox"],
            "bbox_threshold": 0.25,
            "iou_threshold": 0.8,
        }
        return (api_body, image_path)
